package main

import (
	"context"
	"fmt"
	"go_stu/5_grpc_stu/5_metadata_go/protos"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

type Server struct {
	protos.UnimplementedGreeterServer
}

func (s *Server) SayHello(ctx context.Context, request *protos.HelloRequest) (*protos.HelloReply, error) {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		fmt.Println("get metadata error")
	}
	if v, ok := md["name"]; ok {
		fmt.Println(v)
	}
	return &protos.HelloReply{
		Message: "hello, " + request.Name,
	}, nil
}

func main() {
	// 实例化服务对象
	g := grpc.NewServer()
	// 注册服务
	protos.RegisterGreeterServer(g, &Server{})
	// 监听端口
	lis, err := net.Listen("tcp", "0.0.0.0:8083")
	if err != nil {
		panic(err.Error())
	}
	// 开启服务
	if err := g.Serve(lis); err != nil {
		panic("start err" + err.Error())
	}
}
